package com.kaiyidiannao.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.Toast;
import com.kaiyidiannao.activity.ImagePagerActivity;
import com.kaiyidiannao.activity.login_or_registered.LoginOrRegistered;
import com.kaiyidiannao.common.util.DisplayUtil;
import com.kaiyidiannao.common.util.MySystemParams;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.view.ActionBarView;
import com.kaiyidiannao.widget.LoadingDialog;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class BaseActionBarActivity extends FragmentActivity {
    protected ImageLoader imageLoader = ImageLoader.getInstance();// 加载图片
    protected ActionBarView actionBarView; // Actionbar
    protected LoadingDialog dialog;

    protected DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setImageLoaderOption(); // imageloader参数
        initPopWindow();// popwindow
    }

    //------------------------------------图片浏览用start------------------------------------------------------

    protected void setImageLoaderOption() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_default)
                .showImageForEmptyUri(R.drawable.icon_default) //ic_empty
                .showImageOnFail(R.drawable.icon_default) //ic_error
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .displayer(new RoundedBitmapDisplayer(0))
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)////设置图片以如何的编码方式显示
                .build();
    }

    protected void startImagePagerActivity(int position, String[] imageUrls) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(DataCenter.Extra.IMAGES, imageUrls);
        intent.putExtra(DataCenter.Extra.IMAGE_POSITION, position);
        startActivity(intent);
    }
    //------------------------------------图片浏览用end------------------------------------------------------

    /**
     * 判断checkBox弹出的图层是否显示
     * @return
     */
    protected boolean isShownTopBar() {
        return false;
    }

    /**
     * 关闭checkBox弹出的图层
     * @return
     */
    protected void  closeTopBar() {
    }

    /**
     * 打开checkBox弹出的图层
     * @return
     */
    protected void  openTopBar() {
    }
    //---------------------------------------------------------------------------------------
    public void showToast(String str, String defaultTip) {
        if (!TextUtils.isEmpty(str.trim())) {
            Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
        } else {
            if (!TextUtils.isEmpty(defaultTip.trim())) {
                Toast.makeText(this, defaultTip, Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void showToast(int resId) {
        showToast(getString(resId));
    }

    public void showToast(String str) {
        showToast(str, "");
    }

    public void showProgressDialog(String str) {
        if (dialog == null) {
            dialog = new LoadingDialog(this);
            dialog.setCanceledOnTouchOutside(false);
        }
        dialog.setText(str);
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }

    public void showProgressDialog() {
       this.showProgressDialog("正在加载...");
    }

    public void closeProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    //---------------------------------------------------------
    public static boolean isLogin() {
        if (DataCenter.userLogin.size() == 0) {
           return false;
        }
        return true;
    }

    public void startLoginOrRegistered() {
        showToast("请先登录！");
        Intent intent = new Intent(BaseActionBarActivity.this,LoginOrRegistered.class);
        startActivity(intent);
    }

    protected String getUserid() {
        return DataCenter.userLogin.get(0).getUserid();
    }

    //----------------------------------------------------------------------------------
    protected PopupWindow popupWindow;
    protected void initPopWindow() {
        View rootView = getLayoutInflater().inflate(R.layout.dialog, null);
        popupWindow  = new PopupWindow(rootView, MySystemParams.getInstance(this).screenWidth,
                MySystemParams.getInstance(this).screenHeight - DisplayUtil.dip2px(50, this), false);

    }

    protected void showPopWindow() {
        if (popupWindow != null && !popupWindow.isShowing()) {
            popupWindow.showAsDropDown(actionBarView, 0, 0);
            popupWindow.update();
        }
    }

    protected void closePopWindow() {
       if (popupWindow != null && popupWindow.isShowing()) {
           popupWindow.dismiss();
       }
    }
}
